import ScrollViewAdsorptionEffect from "./ScrollViewAdsorptionEffect";
import ScrollViewToolParent from "./ScrollViewToolParent";

const { ccclass, menu, property, requireComponent } = cc._decorator;

@ccclass
@menu("自定义组件/ScrollView/跳转到指定的子节点")
@requireComponent(ScrollViewAdsorptionEffect)
export default class ScrollViewToIndex extends cc.Component {
    @property(cc.ScrollView)
    scrollview: cc.ScrollView = null;
    // @property(cc.Prefab)
    // itemPrefab: cc.Prefab = null;
    _content: cc.Node = null;
    _layout: cc.Layout = null;
    _controlPoint

    _ScrollViewToolParent_Sc: ScrollViewToolParent = null;
    _ScrollViewAdsorptionEffect_Sc: ScrollViewAdsorptionEffect = null;
    onLoad() {
        this._content = this.scrollview.content;
        this._ScrollViewAdsorptionEffect_Sc = this.node.getComponent("ScrollViewAdsorptionEffect")
        this._ScrollViewToolParent_Sc = this.node.getComponent("ScrollViewToolParent")




    }
    onEnable() {


    }
    onDisable() {


    }


    ScrollToIndex(index = 0, maxIndex = 0, dt = 1) {
        if (this._ScrollViewToolParent_Sc) {
            this._ScrollViewToolParent_Sc.setDataByIndex(index, maxIndex);
        }
        if (!this._ScrollViewAdsorptionEffect_Sc) {
            this._controlPoint = cc.v2()
        } else {
            this._controlPoint = this._ScrollViewAdsorptionEffect_Sc._controlPoint
        }

        //查找包含吸引点的节点
        let len = this._content.children.length;
        for (let i = 0; i < len; i++) {
            let item = this._content.children[i];
            if (item && item.index == index) {
                let targetPos = Math.abs(item.y) - (this.scrollview.node.height * 0.5 - this._controlPoint.y)// - this._layout.paddingTop / 2 - this._layout.paddingBottom - this._layout.spacingY / 2
                this.scrollview.scrollToOffset(cc.v2(item.x, targetPos), dt) //向吸引点滑动

                break;
            }

        }
    }

}
